<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('宿舍列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>房间号：</label>
                            <input type="text" name="dormitoryNumber"/>
                        </li>
                        <li>
                            <label>宿舍类型：</label>
                            <select name="dormitoryType" th:with="type=${@dict.getType('dormitory_type')}">
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="dormitory:dormitory:add">
                <i class="fa fa-plus"></i> 添加宿舍
            </a>
            <a class="btn btn-success" onclick="addBoarder()" shiro:hasPermission="dormitory:dormitoryRecord:add">
                <i class="fa fa-plus"></i> 添加住宿人
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <!-- 点击事件的方式打开使用 detailViewIcon: false and detailViewByClick: true
                 data-detail-view-icon="false"
                 data-detail-view-by-click="true" -->
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var prefix = ctx + "dormitory/dormitory";
    var prefixChild = ctx + "dormitory/dormitoryRecord";
    var dormitoryEditFlag = [[${@permission.hasPermi('dormitory:dormitory:edit')}]];
    var dormitoryRemoveFlag = [[${@permission.hasPermi('dormitory:dormitory:remove')}]];
    var dormitoryRecordEditFlag = [[${@permission.hasPermi('dormitory:dormitoryRecord:edit')}]];
    var dormitoryRecordRemoveFlag = [[${@permission.hasPermi('dormitory:dormitoryRecord:remove')}]];
    var dormitoryTypeDatas = [[${@dict.getType('dormitory_type')}]];
    var bedTypeDatas = [[${@dict.getType('bed_type')}]];

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            modalName: "宿舍信息",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            detailView: true,
            onExpandRow: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [{
                checkbox: true
            },
                {
                    field: 'dormitoryId',
                    title: '宿舍编号',
                },
                {
                    field: 'dormitoryType',
                    title: '宿舍类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(dormitoryTypeDatas, value);
                    }
                },
                {
                    field: 'dormitoryNumber',
                    title: '房间号'
                },
                {
                    field: 'galleryful',
                    title: '容纳人数',
                },
                {
                    field: 'freeBed',
                    title: '空闲床位'
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        // actions.push('<a class="btn btn-success btn-xs ' + dormitoryRecordAddFlag + '" href="javascript:void(0)" onclick="addBoarder(\'' + row.dormitoryId + '\')"><i class="fa fa-edit"></i>添加住宿人</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + dormitoryEditFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.dormitoryId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + dormitoryRemoveFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.dormitoryId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });


    initChildTable = function (index, row, $detail) {
        var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
        $(childTable).bootstrapTable({
            url: prefixChild + "/detailList",
            updateUrl: prefixChild + "/edit/{id}",
            removeUrl: prefixChild + "/remove",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",
            queryParams: {
                dormitoryId: row.dormitoryId
            },
            columns: [
                {
                    field: 'recordNumber',
                    title: '记录编号',
                    visible:false
                },
                {
                    field: 'dormitoryNumber',
                    title: '宿舍编号',
                    visible:false
                },
                {
                    field: 'employeeNumber',
                    title: '员工编号'
                },
                {
                    field: 'employeeName',
                    title: '员工姓名'
                },
                {
                    field: 'phoneNumber',
                    title: '手机号码'
                },
                {
                    field: 'department',
                    title: '员工部门'
                },
                {
                    field: 'checkInTime',
                    title: '入住时间'
                },
                {
                    field: 'checkOutTime',
                    title: '退宿时间'
                },
                {
                    field: 'bedType',
                    title: '床位类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(bedTypeDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + dormitoryRecordEditFlag + '" href="javascript:void(0)" onclick="editBoarder(\'' + row.recordNumber + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + dormitoryRecordRemoveFlag + '" href="javascript:void(0)" onclick="removeBoarder(\'' + row.recordNumber + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }
            ]
        });
    };

    function addBoarder() {
        table.set();
        $.modal.open("添加住宿人", "dormitory/dormitoryRecord/add");
    }

    function editBoarder(id) {
        table.set();
        if ($.common.isEmpty(id) && table.options.type == table_type.bootstrapTreeTable) {
            var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
            if ($.common.isEmpty(row)) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            var url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
            $.modal.open("修改" + table.options.modalName, url);
        } else {
            $.modal.open("修改" + table.options.modalName, editBoarderUrl(id));
        }
    }

    // 修改访问地址
    function editBoarderUrl(id) {
        var url = "/404.html";
        if ($.common.isNotEmpty(id)) {
            // url = table.options.updateUrl.replace("{id}", id);
            url = prefixChild + "/edit/{id}".replace("{id}", id);
        } else {
            var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            url = prefixChild + "/edit/{id}".replace("{id}", id);
        }
        return url;
    }

    function removeBoarder(id) {
        console.log(id);
        table.set();
        $.modal.confirm("确定删除该条" + "信息吗？", function () {
            var url = $.common.isEmpty(id) ? prefixChild + "/remove" : prefixChild + "/remove".replace("{id}", id);
            if (table.options.type == table_type.bootstrapTreeTable) {
                $.operate.get(url);
            } else {
                var data = {"ids": id};
                $.operate.submit(url, "post", "json", data);
            }
        });
    }


</script>
</body>
</html>